package com.zuo.demo01.controller.admin;


import com.alibaba.fastjson.JSON;
import com.zuo.demo01.config.Constants;
import com.zuo.demo01.utils.FileUtil;
import com.zuo.demo01.utils.MyBlogUtils;
import com.zuo.demo01.utils.Result;
import com.zuo.demo01.utils.ResultGenerator;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 */
@Controller
public class UploadController {


    @RequestMapping("/blogs/md/uploadfile")//博客内容图片
    @ResponseBody
    public String upload(@RequestParam("editormd-image-file") MultipartFile file,HttpServletRequest httpServletRequest,@RequestParam("dir") String dir) throws Exception {
        Map<String,Object> map = new HashMap<>();

        //重命名
        String newFileName = FileUtil.rename(file.getOriginalFilename());

        String ff = Constants.FILE_IMGS_BLOGS_CONTENT+"/"+dir+"/"+newFileName;

        FileOutputStream fs=new FileOutputStream(ff);
        BufferedOutputStream bs=new BufferedOutputStream(fs);
        fs.write(file.getBytes());//获取上传的图片并保存到指定位置
        bs.close();
        fs.close();

        URI url = new URI(httpServletRequest.getRequestURL() + "");
        map.put("success",1);
        map.put("message","upload success");
        String pic = url.getScheme()+"://"+url.getHost()+":8081/"+Constants.FILE_IMGS_BLOGS_CONTENT_URL+"/"+dir+"/"+newFileName;
        map.put("url",pic);//这键是要展示回markdown的，不可轻易动
        /*http://localhost:8081/imgs/blogs/content/20210227_09472872.jpg*/
        /*System.out.println("http://localhost:8081/imgs/blogs/content/20210227_09505099.jpg");*/
        /*System.out.println(this.getClass().getSimpleName()+"->"+pic);

        System.out.println(this.getClass().getSimpleName()+"->"+new URI(httpServletRequest.getRequestURL() + "").getScheme());
        System.out.println(this.getClass().getSimpleName()+"->"+new URI(httpServletRequest.getRequestURL() + "").getUserInfo());
        System.out.println(this.getClass().getSimpleName()+"->"+new URI(httpServletRequest.getRequestURL() + "").getHost());
        System.out.println(this.getClass().getSimpleName()+"->"+new URI(httpServletRequest.getRequestURL() + "").getPort());
*/        /*System.out.println(this.getClass().getSimpleName()+"->"+map);*/
        String remap = JSON.toJSONString(map);
        return remap;
    }



    @RequestMapping("/blogImg/upload")//博客封面
    @ResponseBody
    public String blog_img_upload(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String,Object> map = new HashMap<>();

        String pic = FileUtil.rename(file.getOriginalFilename());
        String ff = Constants.FILE_IMGS_BLOGS_HEADER+"/"+pic;
        FileOutputStream fs=new FileOutputStream(ff);//云服务器有问题
        BufferedOutputStream bs=new BufferedOutputStream(fs);
        fs.write(file.getBytes());//获取上传的图片并保存到指定位置
        bs.close();
        fs.close();

        map.put("img",pic);
        map.put("code",0);
        map.put("msg","修改成功");
        String remap = JSON.toJSONString(map);
        return remap;
    }





}
